package com.friend.uu;

import android.Manifest;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class RulerActivity extends Activity implements View.OnClickListener {
    EditText et_modify;
    ImageView iv_ruler;

    // 创建活动
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ruler);

        setScreen();
        bindView();
        readDatafile();
        // 检查权限
        String p = Manifest.permission.CAMERA;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(p) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{p}, 555);
            }
        }
        //绘制尺子
        iv_ruler.post(new Runnable() {
            @Override
            public void run() {
                drawRuler();
            }
        });
    }

    // 设置屏幕
    private void setScreen() {
        //保持竖屏
        setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //隐藏标题
        if (getActionBar() != null) {
            getActionBar().hide();
        }
        //设置全屏
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
    }

    // 绑定控件
    private void bindView() {
        et_modify = findViewById(R.id.et_modify);
        iv_ruler = findViewById(R.id.iv_ruler);
        iv_ruler.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                drawRuler();
            }
        });
        findViewById(R.id.btn_modify).setOnClickListener(this);
        findViewById(R.id.btn_ruler_back).setOnClickListener(this);
    }

    // 绘制尺子
    void drawRuler() {
        int bW = iv_ruler.getWidth();
        int bH = iv_ruler.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(bW, bH, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);

        float xDpi = getResources().getDisplayMetrics().xdpi;
        float yDpi = getResources().getDisplayMetrics().ydpi;
        Paint paint = new Paint();
        paint.setTextSize(xDpi / 10);
        paint.setColor(Color.WHITE);
        canvas.drawColor(Color.BLACK);
        int n = 0;
        float startY = 0;
        canvas.drawText(getString(R.string.ruler_unit),
                paint.getTextSize() * 2,
                paint.getTextSize() * 3, paint);
        int modifyValue = et_modify.getText().toString().equals("")
                ? 0 : Integer.parseInt(et_modify.getText().toString());
        while (startY < bH) {
            if (n % 10f == 0) {
                canvas.drawLine(0, startY, xDpi / 3, startY, paint);
                canvas.drawText(String.valueOf(n), xDpi / 3, startY + paint.getTextSize(), paint);
            } else if (n % 5f == 0) {
                canvas.drawLine(0, startY, xDpi / 5, startY, paint);
            } else {
                canvas.drawLine(0, startY, xDpi / 7, startY, paint);
            }
            startY = ++n * yDpi / 25.4f * (1 + modifyValue / 10000f);
        }
        iv_ruler.setImageBitmap(bitmap);
    }

    // 主单击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_modify:
                btn_modify_onClick(v);
                break;
            case R.id.btn_ruler_back:
                finish();
                break;
        }
    }

    // 修正按钮
    public void btn_modify_onClick(View view) {
        Button v = (Button) view;
        boolean bool = et_modify.isEnabled();
        drawRuler();
        if (bool) {
            writeToDatafile();
        }
        et_modify.setEnabled(!bool);
        int id = bool ? R.string.modify : R.string.update;
        v.setText(id);
    }

    private void writeToDatafile() {
        SharedPreferences preferences = getPreferences(MODE_PRIVATE);
        String val = et_modify.getText().toString();
        preferences.edit()
                .putString("ruler", val)
                .apply();

    }

    // 读取数据文件
    private void readDatafile() {
        SharedPreferences preferences = getPreferences(MODE_PRIVATE);
        String val = preferences.getString("ruler", "0");
        et_modify.setText(val);
    }
}
